package com.example.accounting.model;

import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.orm.OrmContext;
import ohos.data.orm.OrmPredicates;

import java.util.List;


public class AccountOperate {
    private static int accountNum = 1;
    private static Context context;

    public AccountOperate() {

    }

    public AccountOperate(Context context) {
        AccountOperate.context = context;
    }


    public OrmContext ormCreateDb() {
        DatabaseHelper databaseHelper = new DatabaseHelper(context);
        return databaseHelper.getOrmContext("AccountStore", "AccountStore.db", AccountStore.class);
    }

    private boolean insert(AccountInfo accountInfo) {
        OrmContext ormContext = ormCreateDb();
        boolean isSuccess = ormContext.insert(accountInfo);
        ormContext.flush();
        System.out.println("insert a account");
        accountNum++;
        return isSuccess;
    }

    public boolean insert(String date, double money, String type, String info, String account) {
        AccountInfo accountInfo = new AccountInfo(accountNum, date, money, type, info, account);
        return insert(accountInfo);
    }

    public List<AccountInfo> selectAll() {
        OrmContext ormContext = ormCreateDb();
        OrmPredicates ormPredicates = ormContext.where(AccountInfo.class);
        List<AccountInfo> results = ormContext.query(ormPredicates);
        ormContext.flush();

        System.out.println("select called, result is " + results.size());
        accountNum = results.size() + 1;
        return results;
    }

    public List<AccountInfo> select(int year, int month) {
        OrmContext ormContext = ormCreateDb();
        OrmPredicates ormPredicates = ormContext.where(AccountInfo.class).beginsWith("date", year + "年" + month);
        List<AccountInfo> results = ormContext.query(ormPredicates);
        ormContext.flush();

        System.out.println("select called, result is " + results.size());
        return results;
    }

    public List<AccountInfo> select(int year) {
        OrmContext ormContext = ormCreateDb();
        OrmPredicates ormPredicates = ormContext.where(AccountInfo.class).beginsWith("date", year + "年");
        List<AccountInfo> results = ormContext.query(ormPredicates);
        ormContext.flush();

        System.out.println("select called, result is " + results.size());
        return results;
    }

    public void delete(int accountId) {
        OrmContext ormContext = ormCreateDb();
        OrmPredicates ormPredicates = ormContext.where(AccountInfo.class).equalTo("accountId", accountId);
        List<AccountInfo> results = ormContext.query(ormPredicates);
        if (results.size() == 0) {
            return;
        }
        ormContext.delete(results.get(0));
        ormContext.flush();
    }

    public void update(int accountId, String date, String type, String info, String money) {
        OrmContext ormContext = ormCreateDb();
        OrmPredicates ormPredicates = ormContext.where(AccountInfo.class).equalTo("accountId", accountId);
        List<AccountInfo> results = ormContext.query(ormPredicates);
        if (results.size() == 0) {
            return;
        }
        AccountInfo accountInfo = results.get(0);
        accountInfo.setDate(date);
        accountInfo.setType(type);
        accountInfo.setInfo(info);
        accountInfo.setMoney(Double.parseDouble(money));

        ormContext.update(accountInfo);
        ormContext.flush();
    }
}
